package daoimpl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import pojo.Carrier;
import pojo.DeviceBrand;
import dao.CarrierDAO;

@Repository("CarrierDAO")
public class CarrierDAOImpl implements CarrierDAO {

	private SessionFactory sessionFactory;
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public List<Carrier> getListCarrier() {
		List<Carrier> ds = null;
		
		Session session = null;
		try{
			session = sessionFactory.getCurrentSession();
		}
		catch(HibernateException ex)
		{
			session = sessionFactory.openSession();
		}
			
        try {
            String hql = "Select ca from Carrier ca";
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
        	session.close();
        }
        return ds;
	}

	public Carrier getCarrierByID(int id) {
		Carrier ca = null;
		
		Session session = null;
		try{
			session = sessionFactory.getCurrentSession();
		}
		catch(HibernateException ex)
		{
			session = sessionFactory.openSession();
		}
			
        try {
            String hql = "Select ca from Carrier ca where ca.idcarrier=:id";
            Query query = session.createQuery(hql);
            query.setParameter("id", id);
            ca = (Carrier)query.uniqueResult();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
        	session.close();
        }
        return ca;
	}

}
